package com.qf.boot1.service.impl;


import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.text.csv.CsvWriter;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.crypto.digest.DigestUtil;
import com.qf.boot1.dao.CustomerDao;
import com.qf.boot1.entity.Customer;
import com.qf.boot1.ex.CustomerException;
import com.qf.boot1.service.CustomerService;
import com.qf.boot1.utils.FileUtils;
import com.qf.boot1.vo.CustomerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @author 刘欣
 * @createTime 2023/1/9 19:13
 * @description
 */
@Service
@Slf4j
public class CustomerServiceImpl implements CustomerService {

    @SuppressWarnings("SpringJavaInjectionPointsAutowiringInspection")
    @Autowired
    private CustomerDao customerDao;
    @Value("${upFilePath}")
    private String upFilePath;

    @Override
    public List<Customer> pageQuery(Integer index, Integer limit) {
        return customerDao.pageQuery(index, limit);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCustomer(CustomerVo customerVo) throws CustomerException {
        Customer customer = new Customer(customerVo.getName(), DigestUtil.md5Hex(customerVo.getPassword()), customerVo.getSex(), customerVo.getDate(), new Date());
        Customer customerName = customerDao.selectCustomerByName(customerVo.getName());
        if (customerName != null) {
            CustomerException customerException = new CustomerException(-1007, "用户名重复");
            throw customerException;
        }
        int insert = customerDao.insert(customer);
        if (insert <= 0) {
            throw new CustomerException(-1006, "用户添加失败");
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Customer> selectAll() {
        List<Customer> customers = customerDao.selectAll();
        return customers;
    }


    @Override
    public String exprot(List<Customer> customers) {
        String filename = UUID.randomUUID().toString() + ".csv";
        String finalFilename = upFilePath + filename;
        CsvWriter writer = CsvUtil.getWriter(finalFilename, CharsetUtil.CHARSET_UTF_8);
        writer.write(customers);
        return filename;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void deleteByPrimaryKey(Long[] ids) throws CustomerException {
        List<Long> longs = Arrays.asList(ids);
        int i = customerDao.deleteByPrimaryKey(longs);
        if (i <= 0) {
            throw new CustomerException(-1005, "删除失败,请重新操作");
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(Customer record) {
        return customerDao.insert(record);
    }

    @Override
    public int insertSelective(Customer record) {
        return customerDao.insertSelective(record);
    }

    @Override
    public Customer selectByPrimaryKey(Integer id) {
        return customerDao.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(Customer record) {
        return customerDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public void updateByPrimaryKey(CustomerVo customerVo) throws CustomerException {
        Integer id = customerVo.getId();
        if (id == null) {
            throw new CustomerException(-1009, "修改用户没有id");
        }
        String passwordMd5 = DigestUtil.md5Hex(customerVo.getPassword());

        Customer customer = new Customer(customerVo.getId(), customerVo.getName(), passwordMd5, customerVo.getSex(), customerVo.getDate(), customerVo.getRegistTime());

        int i = customerDao.updateByPrimaryKey(customer);
        if (i <= 0) {
            throw new CustomerException(-1004, "编辑失败，请重新编辑");
        }
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Customer> getCustomerAll(CustomerVo customerVo) {
        Customer customer = new Customer(customerVo.getName(), customerVo.getSex(), customerVo.getDate());
        return customerDao.getCustomerAll(customer);
    }

    @Override
    @Transactional(propagation = Propagation.SUPPORTS)
    public Customer selectByUsernameAndPassword(String username, String password) {
        String md5HexPasswrod = DigestUtil.md5Hex(password);
        return customerDao.selectByUsernameAndPassword(username, md5HexPasswrod);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int register(CustomerVo customerVo, MultipartFile multipartFile) throws IOException {
        File file = FileUtils.createFile(multipartFile);
        multipartFile.transferTo(file);
        Customer customer = new Customer(customerVo.getName(), DigestUtil.md5Hex(customerVo.getPassword()), customerVo.getSex(), customerVo.getDate(), new Date(), file.getName());
        int i = customerDao.insert(customer);
        return i;
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public void selectCustomer(Customer customer) throws CustomerException {
        Customer customerDB = customerDao.selectCustomer(customer);
        if (customerDB != null) {
            throw new CustomerException(-1001, "用户名重复");
        }
    }


}
